<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="rainbow"/>
      <div class="title-container">
        <h3 class="title">{{ $t('login.title') }}</h3>
      </div>

      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          :placeholder="$t('login.username.placeholder')"
          name="username"
          type="text"
          auto-complete="on"
          prefix-icon="fa fa-user"
        />
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          :type="passwordType"
          v-model="loginForm.password"
          :placeholder="$t('login.password.placeholder')"
          name="password"
          auto-complete="on"
          prefix-icon="fa fa-lock"
          @keyup.enter.native="handleLogin">
          <i slot="suffix" :class="{'fa-eye-slash':passwordType, 'fa-eye': !passwordType}" class="el-input__icon fa" style="cursor: pointer;" @click="showPwd"/>
        </el-input>
      </el-form-item>

      <el-button :class="{'animated shake': isLoginError}" :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">{{ $t('login.login') }}</el-button>

    </el-form>

  </div>
</template>

<script>
import { validateIsPhone } from '@/utils/validate'

export default {
  name: 'Login',
  components: {},
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validateIsPhone(value)) {
        callback(new Error(this.$t('login.username.validate')))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error(this.$t('login.password.validate')))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '13285003378',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      passwordType: 'password',
      loading: false,
      redirect: undefined,
      isLoginError: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
  },
  destroyed() {
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('LoginByUsername', this.loginForm)
            .then(() => {
              this.loading = false
              this.$router.push({ path: this.redirect || '/' })
            })
            .catch(() => {
              this.loginErrorAnimation()
              this.loading = false
            })
        } else {
          this.loginErrorAnimation()
          console.log('error submit!!')
          return false
        }
      })
    },
    loginErrorAnimation() {
      this.isLoginError = true
      setTimeout(() => {
        this.isLoginError = false
      }, 500)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
// $linear_color1: #40bfbf;
// $linear_color2: #01b2c6;
$linear_color1: #ff9700;
$linear_color2: #fd8a00;

/* reset element-ui css */
.login-container {
  .el-input {
    input {
      background: transparent;
      border: 0px;
      color: #283443;
      caret-color: #283443;
    }
    &__prefix {
      color: #283443;
    }
    &__suffix {
      color: #283443;
    }
  }
  .el-form-item {
    background: transparent;
    border-bottom: #dedede 1px solid;
  }

  .el-button {
    background: linear-gradient(to right bottom, $linear_color1, $linear_color2);
    border: 0px;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
// $linear_color1: #40bfbf;
// $linear_color2: #01b2c6;
$linear_color1: #ff9700;
$linear_color2: #fd8a00;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background: linear-gradient(to right bottom, $linear_color1, $linear_color2);

  .login-form {
    width: 520px;
    padding: 35px 35px 15px 35px;
    margin: 180px auto;
    background: rgba(255, 255, 255, 0.5);
    box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24);
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #283443;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: #34495e;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #283443;
    cursor: pointer;
    user-select: none;
  }
}
</style>
